package com.evil.common.application.enums;

import cn.hutool.core.map.MapUtil;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import com.evil.common.core.util.NumberUtils;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 显示格式
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum FormatTypeEnum {

    /**
     * 显示格式
     */
    DEFAULT(0, "默认",
            optional -> optional,
            optional -> optional
    ),

    THOUSANDTH(1, "千分符",
            optional -> optional.map(e -> {
                try {
                    int scale = new BigDecimal(e).scale();
                    String result = NumberFormat.getNumberInstance().format(NumberUtils.toBigDecimal(e, null));

                    int supplyNum = scale;
                    String format = "%s.%s";
                    int index = result.lastIndexOf(".");
                    if (index > -1) {
                        supplyNum = scale - (result.length() - index);
                        format = "%s%s";
                    }

                    if (supplyNum > 0) {
                        StringBuilder zeros = new StringBuilder();
                        for (int i = 0; i < supplyNum; i++) {
                            zeros.append("0");
                        }
                        result = String.format(format, result, zeros);
                    }
                    return result;
                } catch (Exception ex) {
                    return e;
                }
            }),
            optional -> optional.map(e -> e.replaceAll(",", ""))
    ),
    PERCENT(2, "百分号",
            optional -> optional.map(e -> {
                try {
                    BigDecimal bigDecimal = new BigDecimal(e);
                    return bigDecimal.multiply(new BigDecimal("100")).toPlainString() + "%";
                } catch (Exception ex) {
                    return e;
                }
            }),
            optional -> optional.map(e -> {
                e = e.replaceAll("%", "");
                return new BigDecimal(e).multiply(new BigDecimal("0.01")).toPlainString();
            })
    ),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;
    @Getter
    private final Function<Optional<String>, Optional<String>> format;
    @Getter
    private final Function<Optional<String>, Optional<String>> restore;

    public static Optional<FormatTypeEnum> findById(Integer id) {
        if (Objects.isNull(id)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static FormatTypeEnum isExistById(Integer id) {
        return FormatTypeEnum.findById(id).orElseThrow(FormatTypeEnum::getBusinessException);
    }

    public static Stream<FormatTypeEnum> getStream() {
        return Arrays.stream(values());
    }

    public static List<Map<Integer, String>> types() {
        return FormatTypeEnum.getStream().map(e -> MapUtil.of(e.id, e.name)).collect(Collectors.toList());
    }

    public static BusinessException getBusinessException() {
        return new BusinessException(RCodeEnum.ERROR_PARAMETER, "显示格式参数错误");
    }

    public static void main(String[] args) {
        System.out.println(new BigDecimal("100.020").scale());
    }
}
